package com.jiaquyun.jcyx.main

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.UserShops
import com.jiaquyun.jcyx.main.adapter.StatePagerAdapter
import com.module.lemlin.owner.OwnerAbstractActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : OwnerAbstractActivity() {

    companion object {
        const val KEY_ACTION_CART = "KEY_ACTION_CART"
        fun open(action: Int) {
            val bundle = Bundle().apply {
                putInt(KEY_ACTION_CART, action)
            }
            ActivityUtils.startActivity(bundle, MainActivity::class.java)
        }
    }

    private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentResumed(fm: FragmentManager, fragment: Fragment) {
            super.onFragmentResumed(fm, fragment)
            when (fragment) {
                is MainHomeFragment -> {
                    BarUtils.setStatusBarLightMode(
                        fragment.requireActivity(), (fragment.alphaStatus > 180)
                    )
                    val color: Int = Color.argb(fragment.alphaStatus, (255), (255), (255))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), color)
                }
                is MainCateFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.WHITE)
                }
                is MainCartFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.WHITE)
                }
                is MainMineFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (false))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), (0x00000000))
                }
            }
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_main

    override fun initView() {
        val mainFragments: MutableList<Fragment> = mutableListOf(
            MainHomeFragment.newInstance(),
            MainCateFragment.newInstance(),
            MainCartFragment.newInstance(),
            MainMineFragment.newInstance()
        )
        val mainStatePager = StatePagerAdapter(
            supportFragmentManager,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,
            mainFragments
        )
        view_pager_main.offscreenPageLimit = mainFragments.size
        view_pager_main.setScrollAnim(false)
        view_pager_main.adapter = mainStatePager

        nav_view_main.setOnNavigationItemSelectedListener { item ->
            val position = when (item.itemId) {
                R.id.item_main_cate -> 1
                R.id.item_main_cart -> 2
                R.id.item_main_mine -> 3
                else -> 0
            }
            view_pager_main.currentItem = position
            return@setOnNavigationItemSelectedListener true
        }

        supportFragmentManager.registerFragmentLifecycleCallbacks(
            fragmentLifecycleCallbacks,
            (true)
        )
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val action = intent?.extras?.getInt(KEY_ACTION_CART) ?: 0
        if (action == 2) {
            nav_view_main.selectedItemId = R.id.item_main_cart
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onEventMainThread(any: Any) {
        if (any is UserShops) {
            initView()
        }
    }
}